package com.example.design;

import com.example.design.beautiful.unit_test.MockWalletRpcServiceOne;
import com.example.design.beautiful.unit_test.STATUS;
import com.example.design.beautiful.unit_test.Transaction;
import com.example.design.beautiful.unit_test.TransactionLock;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Description TODO
 * @date 2022/9/23 9:39
 * @Author liuzhihui
 * @Version 1.0
 */
@SpringBootTest
public class DesignApplicationTests02 {
    @Test
    public void testToNumber_containsInvalidCharaters() {
        Text text1 = new Text("123a4");
        Assertions.assertNull(text1.toNumber());

        Text text2 = new Text("123 4");
        Assertions.assertNull(text2.toNumber());
    }

    TransactionLock mockLock = new TransactionLock() {
        @Override
        public boolean lock(String id) {
            return true;
        }
        @Override
        public void unlock() {
        }
    };

    @Test
    public void testExecute() {
        Long buyerId = 123L;
        Long sellerId = 234L;
        Long productId = 345L;
        String orderId = "456";
        Transaction transaction = new Transaction(null, buyerId, sellerId, productId, orderId);
        // 使用mock对象来替代真正的RPC服务
        transaction.setWalletRpcService(new MockWalletRpcServiceOne());
        transaction.setTransactionLock(mockLock);
        boolean executedResult = transaction.execute();
        Assertions.assertTrue(executedResult);
        Assertions.assertEquals(STATUS.EXECUTED, transaction.getStatus());
    }


    public void testExecute_with_TransactionIsExpired() {
        Long buyerId = 123L;
        Long sellerId = 234L;
        Long productId = 345L;
        String orderId = "456";
        Transaction transaction = new Transaction(null, buyerId, sellerId, productId, orderId) {
            @Override
            protected boolean isExpired() {
                return true;
            }
        };
        boolean actualResult = transaction.execute();
        Assertions.assertFalse(actualResult);
        Assertions.assertEquals(STATUS.EXPIRED, transaction.getStatus());
    }
}
